package unitTest

import (
	"strings"
)

func Split(str string, sep string) []string {
	var index = strings.Index(str, sep)
	var tmp = make([]string, 0, strings.Count(str, sep)+1)
	//var tmp []string
	var left = str[0:index]
	var right = str[index+len(sep):]
	tmp = append(tmp, left)
	var count = strings.Count(right, sep)
	for i := 0; i < count; i++ {
		index = strings.Index(right, sep)
		left = right[0:index]
		right = right[index+len(sep):]
		tmp = append(tmp, left)
	}
	tmp = append(tmp, right)
	return tmp
}
func Split2(s, sep string) (result []string) {
	result = make([]string, 0, strings.Count(s, sep)+1)
	i := strings.Index(s, sep)
	for i > -1 {
		result = append(result, s[:i])
		s = s[i+len(sep):] // 这里使用len(sep)获取sep的长度
		i = strings.Index(s, sep)
	}
	result = append(result, s)
	return
}

//var tmp []string

func Split3(str string, sep string, tmp []string) []string {
	if len(tmp) == 0 {
		tmp = make([]string, strings.Count(str, sep)+1)
	}
	var index = strings.Index(str, sep)
	//fmt.Println(index)
	if index < 0 {
		tmp = append(tmp, str)
		return tmp
	}
	var left = str[0:index]
	var right = str[index+len(sep):]
	tmp = append(tmp, left)
	return Split3(right, sep, tmp)

	//fmt.Printf("%#v", tmp)
}
